import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/bar_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/space_statistics_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/vo/team_spaces_vo.dart';

class SpaceStatisticsPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '空间统计',
        backgroundColor: bgWhiteColor,
      ),
      body: GetBuilder<SpaceStatisticsPageController>(
        init: SpaceStatisticsPageController(),
        builder: (controller) {
          TeamSpacesData teamSpacesData = controller.teamSpacesData.value;
          TeamData teamData = teamSpacesData.item!;
          StatisticsItem statistics = teamSpacesData.statistics!;
          ListDataItem seckill = teamSpacesData.seckill!;
          ListDataItem? team = teamSpacesData.team;
          return ScrollConfigurationCommon(child: ListView(
            children: [
              _itemBody(title: '${teamSpacesData.title}', children: [
                _msgItem(
                  title: '${teamData.name}', 
                  value: '${teamData.createdAt}',
                  color: textSubtitleColor,
                  fontWeight: FontWeight.normal
                )
              ]),
              _itemBody(title: '${statistics.title}', children: statistics.item!.map((item) {
                return Column(
                  children: item.map((dataItem) {
                    return _msgItem(
                      title: '${dataItem.label}', 
                      value: '${dataItem.value}'
                    );
                  }).toList(),
                );
              }).toList()),
              _itemBody(title: '${seckill.title}', children: seckill.item!.map((item) {
                return _msgItem(
                  title: '${item.label}', 
                  value: '${item.value}'
                );
              }).toList()),
              team != null ? _itemBody(title: '${team.title}', children: team.item!.map((item) {
                return _msgItem(
                  title: '${item.label}', 
                  value: '${item.value}'
                );
              }).toList()) : Container(),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                margin: EdgeInsets.only(top: 12.h),
                child: InkWellUtil.shakeProofInkWell(Container(
                  padding: EdgeInsets.symmetric(horizontal: 12.w),
                  decoration: BoxDecoration(
                    color: bgWhiteColor,
                    borderRadius: BorderRadius.circular(5.w)
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      BarUtil(title: '团队表现', padding: 0, fontSize: 16.sp),
                      Image.asset(
                        'assets/icons/icon_arrow_right.png',
                        width: 14.w,
                        height: 14.w,
                      )
                    ],
                  ),
                ), () {
                  Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.SPACE_STATISTICS_PAGE}${AppRouters.SPACE_STATISTICS_DETAIL_PAGE}');
                }),
              )
            ],
          ));
        },
      ),
    );
  }

  Widget _itemBody({required String title, required List<Widget> children}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      margin: EdgeInsets.only(top: 12.h),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 8.w),
        decoration: BoxDecoration(
          color: bgWhiteColor,
          borderRadius: BorderRadius.circular(5.w)
        ),
        child: Column(
          children: [
            Container(
              height: 54.h,
              padding: EdgeInsets.symmetric(horizontal: 4.w),
              decoration: BoxDecoration(
                border: Border(
                  bottom: children.length <= 0 ? BorderSide.none : BorderSide(
                    width: .5.h,
                    color: lineColor
                  )
                )
              ),
              child: BarUtil(title: title, padding: 0, fontSize: 16.sp),
            ),
            Container(
              padding: EdgeInsets.only(left: 4.w, right: 4.w, bottom: children.length <= 0 ? 0 : 16.h),
              child: Column(
                children: children,
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _msgItem({ required String title, required String value, Color? color, FontWeight fontWeight = FontWeight.bold }) {
    return Container(
      margin: EdgeInsets.only(top: 16.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: TextStyle(
              color: textBlackColor,
              fontSize: 14.sp
            ),
          ),
          Text(
            value,
            style: TextStyle(
              color: color ?? mainTextColor,
              fontSize: 14.sp,
              fontWeight: fontWeight
            ),
          )
        ],
      ),
    );
  }
}